package com.start.platform.core.controller;

import com.alibaba.fastjson2.JSONObject;
import com.start.common.api.Result;
import com.start.common.details.BaseUserDetails;
import com.start.platform.core.StartPlatformManage;
import com.start.platform.core.service.StartPlatformUserService;
import com.start.platform.core.vo.StartPlatformFansDetail;
import com.start.platform.core.vo.StartPlatformUserLoginModel;
import com.start.security.StartTokenTools;
import com.start.security.annotations.IgnoreAuth;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Tag(name = "第三方平台api")
@RequestMapping("/start/platformApi")
public class StartPlatformApiController {

    @Autowired
    private StartPlatformUserService platformUserService;

    @IgnoreAuth
    @Operation(summary = "平台验证token")
    @RequestMapping("/platformVerifyToken/{platformType}/{appId}")
    public Object platformVerifyToken(@PathVariable("platformType") String platformType, @PathVariable("appId") String appId) {
        return StartPlatformManage.getInstance().platformVerifyToken(platformType, appId);
    }

    @IgnoreAuth
    @Operation(summary = "第三方平台用户登录")
    @PostMapping("/login/{platformType}/{appId}")
    public Result<?> login(@PathVariable("platformType") String platformType, @PathVariable("appId") String appId, String code, @RequestBody JSONObject prop) {
        return Result.ok(StartPlatformManage.getInstance().login(platformType, appId, code, prop));
    }

    @Operation(summary = "当前账号绑定其他平台账号")
    @GetMapping("/bindOtherPlatform/{platformType}/{appId}/{platformOpenId}")
    public Result<?> bindOtherPlatform(@PathVariable("platformType") String platformType,
                                       @PathVariable("appId") String appId,
                                       @PathVariable("platformOpenId") String platformOpenId) {
        BaseUserDetails currentUser = StartTokenTools.getCurrentUser();
        StartPlatformManage.getInstance().bindOtherPlatform(currentUser, platformType, appId, platformOpenId);
        return Result.ok();
    }

    @IgnoreAuth
    @Operation(summary = "根据第三方平台code获取平台用户信息")
    @PostMapping("/getUserInfoByPlatformCode/{platformType}/{appId}/{code}")
    public Result<?> getUserInfoByPlatformCode(@PathVariable("platformType") String platformType,
                                               @PathVariable("appId") String appId, @PathVariable("code") String code, @RequestBody JSONObject prop) {
        return Result.ok(StartPlatformManage.getInstance().getUserInfoByPlatformCode(platformType, appId, code, prop));
    }

    @IgnoreAuth
    @Operation(summary = "根据第三方平台code获取平台用户信息")
    @PostMapping("/saveFans/{platformType}/{appId}")
    public Result<?> saveFans(@PathVariable("platformType") String platformType,
                              @PathVariable("appId") String appId,
                              @RequestBody StartPlatformFansDetail fans) {
        StartPlatformManage.getInstance().saveFans(platformType, appId, fans);
        return Result.ok();
    }


    @Operation(summary = "根据第三方平台code获取平台用户信息")
    @PostMapping("/saveFansAndBind/{platformType}/{appId}")
    public Result<?> saveFansAndBind(@PathVariable("platformType") String platformType,
                                     @PathVariable("appId") String appId,
                                     boolean autoBind, @RequestBody StartPlatformFansDetail fans) {
        StartPlatformManage.getInstance().saveFansAndBind(platformType, appId, autoBind, fans);
        return Result.ok();
    }


    @IgnoreAuth
    @Operation(summary = "手机端用户账号登录")
    @PostMapping("/accountLogin/{platformType}/{appId}")
    public Result<?> accountLogin(@RequestBody StartPlatformUserLoginModel loginModel,
                                  @PathVariable("platformType") String platformType,
                                  @PathVariable("appId") String appId) {
        return Result.ok(StartPlatformManage.getInstance().login(platformType, appId, loginModel));
    }


    @IgnoreAuth
    @Operation(summary = "注册或登录")
    @PostMapping("/saveFansAndLogin/{platformType}/{appId}")
    public Result<?> saveFansAndLogin(@PathVariable("platformType") String platformType,
                                      @PathVariable("appId") String appId,
                                      @RequestBody StartPlatformFansDetail fans) {
        return Result.ok(StartPlatformManage.getInstance().saveFansAndLogin(platformType, appId, fans));
    }

}
